package com.monitor.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class TPMonitorUtil {

    public static LinkedBlockingQueue<Long> perSecondRunTimeQueueA = new LinkedBlockingQueue<Long>();

    public static LinkedBlockingQueue<LinkedBlockingQueue<Long>> perMinuteQueueA = new LinkedBlockingQueue<LinkedBlockingQueue<Long>>(60);

    public static LinkedBlockingQueue<Long> perSecondRunTimeQueueB = new LinkedBlockingQueue<Long>();

    public static LinkedBlockingQueue<LinkedBlockingQueue<Long>> perMinuteQueueB = new LinkedBlockingQueue<LinkedBlockingQueue<Long>>(60);

    public static LinkedBlockingQueue<Long> perSecondRunTimeQueueC = new LinkedBlockingQueue<Long>();

    public static LinkedBlockingQueue<LinkedBlockingQueue<Long>> perMinuteQueueC = new LinkedBlockingQueue<LinkedBlockingQueue<Long>>(60);

    public static void gatherVeryMinutes() {
        new Thread(new Runnable() {
            public void run() {
                while(true) {
                    if(perMinuteQueueA.size()==60){
                        perMinuteQueueA.poll();
                    }
                    LinkedBlockingQueue<Long> tempSecondRunTimeQueueA =  new LinkedBlockingQueue<Long>();
                    tempSecondRunTimeQueueA.addAll(perSecondRunTimeQueueA);
                    perMinuteQueueA.offer(tempSecondRunTimeQueueA);

                    if(perMinuteQueueB.size()==60){
                        perMinuteQueueB.poll();
                    }
                    LinkedBlockingQueue<Long> tempSecondRunTimeQueueB =  new LinkedBlockingQueue<Long>();
                    tempSecondRunTimeQueueB.addAll(perSecondRunTimeQueueB);
                    perMinuteQueueB.offer(tempSecondRunTimeQueueB);

                    if(perMinuteQueueC.size()==60){
                        perMinuteQueueC.poll();
                    }
                    LinkedBlockingQueue<Long> tempSecondRunTimeQueueC =  new LinkedBlockingQueue<Long>();
                    tempSecondRunTimeQueueC.addAll(perSecondRunTimeQueueC);
                    perMinuteQueueC.offer(tempSecondRunTimeQueueC);
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    public static Long getTP(LinkedBlockingQueue<Long> perSecondRunTimeQueue,LinkedBlockingQueue<LinkedBlockingQueue<Long>> perMinuteQueue,double num){

        if(perMinuteQueue.size()<60){

            List<Long> perSecondRunTimeList = new ArrayList<>(perSecondRunTimeQueue);
            Collections.sort(perSecondRunTimeList);

            Integer index = (int)Math.ceil(perSecondRunTimeList.size() * num);
            return perSecondRunTimeList.get(index-1);
        }

        LinkedBlockingQueue<Long> peek = perMinuteQueue.peek();

        List<Long> peekList = new ArrayList<>(peek);

        List<Long> perSecondRunTimeList = new ArrayList<>(perSecondRunTimeQueue);
        perSecondRunTimeList = perSecondRunTimeList.subList(peekList.size(), perSecondRunTimeList.size());

        Collections.sort(perSecondRunTimeList);

        Integer index = (int)Math.ceil(perSecondRunTimeList.size() * num);

        return perSecondRunTimeList.get(index-1);

    }

}
